﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PFMCandidate;

namespace PFMHandlers
{
    public class PFMEncodeHandler:PFMHandler
    {
        private static string sEncodeCode = "POODlwCSharp";
        public override byte[] Perform(Candidate aCandidate, byte[] baTarget)
        {
            byte[] sResult = null;
            base.Perform(aCandidate, baTarget);
            sResult = GetProcessingData(aCandidate, baTarget);
            sResult = DoEncodeData(aCandidate, sResult);
            return sResult;
        }

        private byte[] DoEncodeData(Candidate aCandidate, byte[] sResult)
        {
            PFMZipHandler aHandler = null;
            int iCodeLength;
            byte xorValue;
            aHandler = new PFMZipHandler();
            sResult = aHandler.Perform(aCandidate, sResult);
            iCodeLength = sEncodeCode.Length;
            for (int iCount = 0; iCount < sResult.Length - 1; iCount++)
            {
                xorValue = (byte)sEncodeCode[iCount % iCodeLength];
                sResult[iCount] = (byte)(sResult[iCount] ^ xorValue);
            }
            ChangeCandidateProcessedName(aCandidate);
            return sResult;
        }

        protected override void ChangeCandidateProcessedName(PFMCandidate.Candidate aCandidate)
        {
            //throw new NotImplementedException();
            //Candidate.ProcessedName = aCandidate.Name;
            base.ChangeCandidateProcessedName(aCandidate);
            aCandidate.ProcessedName += ".XOR";
        }
    }
}
